<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
            padding-left: 30px;
        }
        .box{
            width: 400px;
            height: 280px;
            border: 1px solid black;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="box" ref="myChart"></div>
        
        <my-echarts class="box" :options="chartOptions"></my-echarts>

        <!-- 
            ref 属性定义在HTML标签上，通过this.$refs 加载的是DOM对象
            ref 属性定义在组件标签上，通过this.$refs 加载的是组件实例对象
         -->
        <my-echarts class="box" :options="tempOptions" ref="chartComp"></my-echarts>
        <input type="button" value="加载图表数据" @click="setChartData()">

    </div>
    <script type="module">
        import * as echarts from "../../assets/echarts/echarts.esm.js";
        // 加载 Vue3 的ESM 模块文件
        import { createApp } from "../../assets/vue3/vue.esm-browser.js"
        import MyEcharts from "./components/MyEcharts.js";
        createApp({
            components: {
                MyEcharts,
            },
            data(){
                return {
                    chartOptions: {
                        title: {
                            text: '标题'
                        },
                        tooltip: {},
                        legend: {
                            data: ['销量']
                        },
                        xAxis: {
                            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                        },
                        yAxis: {},
                        series: [
                            {
                                name: '销量',
                                type: 'bar',
                                data: [5, 20, 36, 10, 10, 20]
                            }
                        ]
                    },
                    tempOptions:{}
                }
            },
            methods: {
                initEcharts() {
                    this.$myChart = echarts.init(this.$refs.myChart);
                    this.setOption(this.chartOptions);
                },
                setOption(options){
                    this.$myChart.setOption(options);
                },
                setChartData(){
                    // console.log(this.$refs.chartComp);
                    this.$refs.chartComp.showLoading();
                    setTimeout(() => {
                        this.tempOptions = {
                            title: {
                                text: '标题'
                            },
                            tooltip: {},
                            legend: {
                                data: ['销量']
                            },
                            xAxis: {
                                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                            },
                            yAxis: {},
                            series: [
                                {
                                    name: '销量',
                                    type: 'bar',
                                    data: [5, 20, 36, 10, 10, 20]
                                }
                            ]
                        }
                        this.$refs.chartComp.hideLoading();
                    }, 3000);

                }
            },
            mounted () {
                this.initEcharts();
            },
        }).mount("#app")
    </script>
</body>
</html>